/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DetallReservaPanel.java
 *
 * Created on 09-dic-2011, 17:28:50
 */
package edu.uoc.tdp.pac4.only5.gui.client.panels.connexio;

import edu.uoc.tdp.pac4.only5.domain.Booking;
import edu.uoc.tdp.pac4.only5.domain.Payment;
import edu.uoc.tdp.pac4.only5.domain.Resource;
import edu.uoc.tdp.pac4.only5.domain.Space;
import edu.uoc.tdp.pac4.only5.domain.User;
import edu.uoc.tdp.pac4.only5.exceptions.EespaisDatabaseException;
import edu.uoc.tdp.pac4.only5.gui.client.panels.AbstractPanel;
import edu.uoc.tdp.pac4.only5.manager.PaymentManager;
import edu.uoc.tdp.pac4.only5.manager.ResourceManager;
import edu.uoc.tdp.pac4.only5.manager.SpaceManager;
import edu.uoc.tdp.pac4.only5.manager.UserManager;
import edu.uoc.tdp.pac4.only5.tools.DateTool;
import edu.uoc.tdp.pac4.only5.vo.ResourceBookingVO;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.AbstractListModel;

/**
 *
 * @author Gerard
 */
public class DetallReservaPanel extends AbstractPanel {

    private UserManager userManager;
    private PaymentManager paymentManager;
    private SpaceManager spaceManager;
    private ResourceManager resourceManager;
    private List<Resource> resources;
    private Booking booking;

    public DetallReservaPanel(UserManager userManager, PaymentManager paymentManager, SpaceManager spaceManager, ResourceManager resourceManager, Booking booking) {
        this.userManager = userManager;
        this.paymentManager = paymentManager;
        this.spaceManager = spaceManager;
        this.resourceManager = resourceManager;
        this.booking = booking;
        resources = new ArrayList<Resource>();
        initComponents();
        setValues();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblCustomer = new javax.swing.JLabel();
        txtCustomer = new javax.swing.JTextField();
        txtSpace = new javax.swing.JTextField();
        lblSpace = new javax.swing.JLabel();
        txtStartTime = new javax.swing.JTextField();
        lblStartTime = new javax.swing.JLabel();
        txtEndTime = new javax.swing.JTextField();
        lblEndTime = new javax.swing.JLabel();
        txtPax = new javax.swing.JTextField();
        lblPax = new javax.swing.JLabel();
        txtStatus = new javax.swing.JTextField();
        lblStatus = new javax.swing.JLabel();
        lblExtraResources = new javax.swing.JLabel();
        txtCancelTime = new javax.swing.JTextField();
        lblCancelTime = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        listExtraResources = new javax.swing.JList();
        txtCancelUser = new javax.swing.JTextField();
        lblCancelUser = new javax.swing.JLabel();
        txtTotalPayment = new javax.swing.JTextField();
        lblTotalPayments = new javax.swing.JLabel();
        lblTitleConsultaReserves = new javax.swing.JLabel();

        lblCustomer.setText("Customer");

        lblSpace.setText("Space");

        lblStartTime.setText("Start Time");

        lblEndTime.setText("End Time");

        lblPax.setText("Pax");

        lblStatus.setText("Status");

        lblExtraResources.setText("Extra Resources");

        lblCancelTime.setText("Cancel Time");

        listExtraResources.setEnabled(false);
        jScrollPane1.setViewportView(listExtraResources);

        lblCancelUser.setText("Cancel User");

        txtTotalPayment.setEnabled(false);

        lblTotalPayments.setText("Total Payments");

        lblTitleConsultaReserves.setFont(new java.awt.Font("Lucida Grande", 0, 18));
        lblTitleConsultaReserves.setText("Detall de la reserva");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblCustomer)
                            .addComponent(lblSpace)
                            .addComponent(lblStartTime)
                            .addComponent(lblEndTime)
                            .addComponent(lblPax)
                            .addComponent(lblStatus))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtStatus)
                            .addComponent(txtPax)
                            .addComponent(txtCustomer, javax.swing.GroupLayout.DEFAULT_SIZE, 184, Short.MAX_VALUE)
                            .addComponent(txtSpace)
                            .addComponent(txtStartTime)
                            .addComponent(txtEndTime)))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(lblTitleConsultaReserves)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(lblCancelTime)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(txtCancelTime, javax.swing.GroupLayout.DEFAULT_SIZE, 171, Short.MAX_VALUE))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(lblCancelUser)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(txtCancelUser, javax.swing.GroupLayout.DEFAULT_SIZE, 170, Short.MAX_VALUE))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(lblTotalPayments)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(txtTotalPayment, javax.swing.GroupLayout.DEFAULT_SIZE, 152, Short.MAX_VALUE))))
                .addGap(42, 42, 42)
                .addComponent(lblExtraResources)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(32, 32, 32))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblTitleConsultaReserves)
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(lblExtraResources)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblCustomer)
                            .addComponent(txtCustomer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtSpace, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblSpace))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblStartTime)
                            .addComponent(txtStartTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtEndTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblEndTime))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblPax)
                            .addComponent(txtPax, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblStatus)
                            .addComponent(txtStatus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblCancelTime)
                            .addComponent(txtCancelTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblCancelUser)
                            .addComponent(txtCancelUser, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblTotalPayments)
                            .addComponent(txtTotalPayment, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jScrollPane1))
                .addGap(33, 33, 33))
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblCancelTime;
    private javax.swing.JLabel lblCancelUser;
    private javax.swing.JLabel lblCustomer;
    private javax.swing.JLabel lblEndTime;
    private javax.swing.JLabel lblExtraResources;
    private javax.swing.JLabel lblPax;
    private javax.swing.JLabel lblSpace;
    private javax.swing.JLabel lblStartTime;
    private javax.swing.JLabel lblStatus;
    private javax.swing.JLabel lblTitleConsultaReserves;
    private javax.swing.JLabel lblTotalPayments;
    private javax.swing.JList listExtraResources;
    private javax.swing.JTextField txtCancelTime;
    private javax.swing.JTextField txtCancelUser;
    private javax.swing.JTextField txtCustomer;
    private javax.swing.JTextField txtEndTime;
    private javax.swing.JTextField txtPax;
    private javax.swing.JTextField txtSpace;
    private javax.swing.JTextField txtStartTime;
    private javax.swing.JTextField txtStatus;
    private javax.swing.JTextField txtTotalPayment;
    // End of variables declaration//GEN-END:variables

    @Override
    protected void setLabels() {
        lblCancelTime.setText(getText("detallreserva.canceltime"));
        lblCancelUser.setText(getText("detallreserva.canceluser"));
        lblCustomer.setText(getText("detallreserva.customer"));
        lblEndTime.setText(getText("detallreserva.endtime"));
        lblExtraResources.setText(getText("detallreserva.extraresources"));
        lblPax.setText(getText("detallreserva.pax"));
        lblSpace.setText(getText("detallreserva.space"));
        lblStartTime.setText(getText("detallreserva.starttime"));
        lblStatus.setText(getText("detallreserva.status"));
        lblTotalPayments.setText(getText("detallreserva.totalpayments"));
    }

    private void setValues() {
        try {
            txtCancelTime.setText(DateTool.getInstance().convertDateToString(booking.getCancelTime()));
            setUserCancelName();
            setUserName();
            txtEndTime.setText(DateTool.getInstance().convertDateToString(booking.getEndTime()));
            txtPax.setText(booking.getPax().toString());
            setSpaceName();
            txtStartTime.setText(DateTool.getInstance().convertDateToString(booking.getStartTime()));
            txtStatus.setText(booking.getStatus());
            setTotalPayments();
            setExtraResources();
        }catch (RemoteException ex) {
            showErrorMessageDialog(this, "error.generic.rmi");
        } catch(EespaisDatabaseException ex){ 
            showErrorMessageDialog(this, "error.generic.databaseerror");
        } 
    }

    private void setUserName() throws RemoteException, EespaisDatabaseException {
        User user = userManager.getUserById(booking.getCustomer());
        if (user != null) {
            txtCustomer.setText(user.toString());
        }
    }

    private void setUserCancelName() throws RemoteException, EespaisDatabaseException {
        User user = userManager.getUserById(booking.getCancelUser());
        if (user != null) {
            txtCancelUser.setText(user.toString());
        }
    }

    private void setSpaceName() throws RemoteException, EespaisDatabaseException {
        Space selectedSpace = null;
        List<Space> spaces = spaceManager.getSpaces();
        //TODO:Gerard, Cambiar això quan l'alex implementi el metode getSpace?
        for (Space space : spaces) {
            if (space.getSpaceId().equals(booking.getSpace())) {
                selectedSpace = space;
                break;
            }
        }
        if (selectedSpace != null) {
            txtSpace.setText(selectedSpace.toString());
        }

    }

    private void setExtraResources() throws RemoteException, EespaisDatabaseException {
        List<ResourceBookingVO> resourcesBooking = resourceManager.getResourceBooking(booking.getId());
        for (ResourceBookingVO resourceVO : resourcesBooking) {
            resources.add(resourceVO.getResource());
        }
        listExtraResources.setModel(new AbstractListModel() {

            @Override
            public int getSize() {
                return resources.size();
            }

            @Override
            public Object getElementAt(int i) {
                return resources.get(i);
            }
        });
    }

    private void setTotalPayments() throws RemoteException, EespaisDatabaseException {
        Payment payment = paymentManager.getPaymentbyId(booking.getId());
        if (payment != null && payment.getPrice() != null) {
            txtTotalPayment.setText(payment.getPrice().toString());
        }
    }
}
